<template>
	<view class="topic">
		<view class="videoBox">
			<view class="imageBox" v-if="imageUrl">
				<image @click="previewImage" style="height: 200rpx;width: 200rpx;" :src="'http://localhost:3000/'+imageUrl" mode=""></image>
				<view class="removeImage" @click="deleteImage()">
					<uni-icons type="clear" color="red" size="30"></uni-icons>
				</view>
			</view>
			<view v-else class="addIcon" @click="selectVideo">
				<uni-icons type="plusempty" color="#CCCCCC" size="30"></uni-icons>
			</view>
		</view>
		<view class="topicTitle">
			<input maxlength="20" v-model="form.title" class="uni-input" focus placeholder="添加标题" />
		</view>
		
		<view class="topicContent">
			<textarea v-model="form.content"  placeholder="说些什么呢" />
		</view>
		<view class="btnBox">
			<button :disabled="disabled" style="color:#ffffff;background:#FF0000;font-size: 30rpx;" type="default"
				@click="submit">发布话题</button>
		</view>
	</view>
</template>

<script>
	import {
		sendTopic
	} from "@/api/index.js"
	export default {
		data() {
			return {
				form:{
					title: '',
					content:''
				},
				disabled:false,
				userInfo:{},
				list: [],
				imageUrl: '',
				schedule: 0,
				uploadStatus: false,
				timer: null,
				limitBtnStatus: 0,
				timeRange: {
					start: '',
					end: ''
				}
			};
		},
		async onLoad() {
			let uInfo = uni.getStorageSync('mini-userinfo')
			if (uInfo) {
				this.userInfo = uInfo
			}
		
		},
		methods:{
			deleteImage() {
				uni.showModal({
					title: '提示信息',
					content: '确定要删除这个图片吗？',
					success: res => {
						if (res.confirm) {
							// 模拟服务器接口
							this.list = []
							this.imageUrl = ''
						}
					}
				});
			},
			previewImage() {
				wx.previewMedia({
					sources: this.list, // 需要预览的资源列表
					current: 0, // 当前显示的资源序号
					url:'http://localhost:3000/'+ this.imageUrl, // 当前预览资源的url链接
					fail(err) {
						console.log(err, '??????????');
					}
				})
			},
			selectVideo() { //图片上传
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						uni.showLoading({
							title: '图片上传中',
							mask: true
						})
						console.log(res, JSON.stringify(res.tempFilePaths));
						const uploadTask = uni.uploadFile({
							url: `${uni.$http.baseUrl}/api/upload/image`,
							method: "POST",
							filePath: res.tempFilePaths[0],
							name: 'image',
							header: {
								"content-type": 'multipart/form-data',
							},
							success: res => {
								console.log(res, '长');
								console.log(JSON.parse(res.data), '成功后的结果');
								let data = JSON.parse(res.data)
								this.list[0] = {
									url:'http://localhost:3000/'+ data.data.fileUrl,
									name: data.data.filename,
									type: 'image'
								}
								this.imageUrl = data.data.fileUrl
							},
							fail: err => {
								uni.showToast({
									title: '视频上传失败',
									icon: 'none'
								})
							},
							complete: () => {
								uni.hideLoading()
							}
						})
					}
				});
			},
			async submit() { //表单提交
				this.disabled = true
				if (this.form.title === '') {
					this.disabled = false
					return uni.showToast({
						title: '请输入标题',
						icon: 'none',
					})
				}
				if (!this.form.content==='') {
					this.disabled = false
					return uni.showToast({
						title: '请输入内容',
						icon: 'none',
					})
				}
				const params = {
					userId: this.userInfo.id,
					content: this.form.content,
					title: this.form.title,
					mediaUrl:this.imageUrl
				}
				console.log(params);
				const topicRes = await sendTopic(params)
				console.log(topicRes, 'topicRes.isok');
				if (topicRes.statusCode===200) {
					uni.showToast({
						title: '发布成功',
						icon: 'none',
						duration: 1000,
					})
					setTimeout(() => {
						uni.navigateBack()
					}, 1000)
				}else{
					uni.showToast({
						title: topicRes.data.errMsg,
						icon: 'none',
						duration: 1000,
					})
				}
			},
		}
	}
</script>

<style lang="scss">
	.topic{
		width: 100%;
		padding: 10rpx;
		font-size: 36rpx;
	}
	.videoBox {
		width: 100%;
		height: 240rpx;
		margin: 20rpx;
		.imageBox{
			width: 200rpx;
			height: 200rpx;
			position: relative;
			.removeImage{
				z-index: 99;
				// border: 2rpx solid #cccccc;
				position: absolute;
				top: -30rpx;
				right: -30rpx;
				display: flex;
				justify-content: center;
				align-items: center;
			}
		}
		.addIcon{
			width: 200rpx;
			height: 200rpx;
			background-color: #F8F8F8;
			border: 2rpx solid #EAEAEA;
			display: flex;
			align-items: center;
			justify-content: center;
			border-radius: 10rpx;
		}
		.videoImg {
			width: 500rpx;
			height: 100%;
			display: flex;
			justify-content: center;
			position: relative;
			background-color: #000000;
			border-radius: 10rpx;
			overflow: hidden;
	
			.trend-video {
				height: 100%;
			}
	
			.img {
				height: 100%;
			}
	
			.removeVideo {
				// width: 70rpx;
				// height: 70rpx;
				// background: #000000;
				// opacity: .4;
				z-index: 99;
				// border: 2rpx solid #cccccc;
				position: absolute;
				top: 0;
				right: 0;
				display: flex;
				justify-content: center;
				align-items: center;
			}
		}
	
	}
	.newAddImg {
		width: 500rpx;
		height: 300rpx;
		border: 2rpx dotted #cccccc;
	
		.addIconBox,
		.progressBox {
			width: 100%;
			height: 100%;
			padding: 0 20rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			flex-direction: column;
	
			.select-tips {
				width: 100%;
				height: 10rpx;
				margin-bottom: 12rpx;
			}
		}
	}
	.topicTitle{
		display: flex;
		color: #000000;
		padding: 0 20rpx;
		.uni-input{
			width: 100%;
		}
	}
	.topicContent {
		width: 100%;
		height: 300rpx;
		padding: 20rpx;
		font-size: 32rpx;
		// border: 2rpx solid #cccccc;
	}
	.btnBox {
		width: 100%;
		padding: 40rpx;
		position: fixed;
		bottom: 0;
	}
</style>
